package zut.cs.liziflower.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zut.cs.liziflower.entity.Flower;
import zut.cs.liziflower.entity.Language;
import zut.cs.liziflower.entity.Result;
import zut.cs.liziflower.service.impl.FlowerCategoryServiceImpl;
import zut.cs.liziflower.service.impl.FlowerServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuwenke
 * @since 2022-05-16
 */
@RestController
@RequestMapping("/api/flower")
public class FlowerController {

    @Resource
    private FlowerServiceImpl flowerService;
    @Resource
    private FlowerCategoryServiceImpl flowerCategory;


    @GetMapping("/flowers")
    public Result<?> getflowers(@RequestParam String name) {
        System.out.println(name);

        List<Flower> list = flowerService.getFlowers(name);

        return Result.success(list);
    }

    @GetMapping("/get-flower-detail")
    public Result<?> getFlowerByFlowerId(@RequestParam int flowerid) {
        Flower flower = flowerService.getDetail(flowerid);
        if (flower != null) {
            QueryWrapper wrapper = new QueryWrapper();

//            flowerCategory.getOne(wrapper.eq("name",flower.getName()))
            return Result.success(flower);
        }
        return Result.error("404", "未找到", null);
    }

    @GetMapping("/get-all")
    public Result<?> getAllFlower() {
        List<Flower> list = flowerService.getAllFlower();
        if (list != null) {
            return Result.success(list);
        }
        return Result.error("404", "未找到", null);
    }

    @GetMapping("/get-page-cart")
    public IPage<Flower> getPage(@RequestParam Integer userid,
                                  @RequestParam Integer pageSize,
                                  @RequestParam Integer pageNum) {
        IPage<Flower> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Flower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid );
        return flowerService.page(page, queryWrapper);
    }

    @GetMapping("/get-flower-language")
    public Result<?> getFlowerLanguage(){

        List<Language> list = flowerService.getFlowerLanguage();
        return Result.success(list);
    }

    @GetMapping("/get-one-language")
    public Result<?> getOneLanguage(@RequestParam("find") String find){
        //模糊查询
        List<Language> language = flowerService.getOneLanguage(find);
        return Result.success(language);
    }


}
